from django.db import models
from dvadmin.utils.models import CoreModel
from supplier.models import SupplierProduct

class PurchasePlan(CoreModel):
    """采购计划"""
    APPROVAL_STATUS = (
        (0, '待审批'),
        (1, '已审批'),
        (2, '已拒绝'),
    )
    
    title = models.CharField(max_length=100, verbose_name="计划标题")
    plan_date = models.DateField(verbose_name="计划日期")
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="总金额")
    status = models.IntegerField(choices=APPROVAL_STATUS, default=0, verbose_name="审批状态")
    remark = models.TextField(null=True, blank=True, verbose_name="备注")

    class Meta:
        db_table = "purchase_plan"
        verbose_name = '采购计划'
        verbose_name_plural = verbose_name
        ordering = ('-create_datetime',)

class PurchaseDetail(CoreModel):
    """采购明细"""
    plan = models.ForeignKey(PurchasePlan, on_delete=models.CASCADE, verbose_name="采购计划")
    product = models.ForeignKey(SupplierProduct, on_delete=models.SET_NULL, verbose_name="供应商产品", null=True, blank=True)
    product_name = models.CharField(max_length=100, verbose_name="商品名称")
    product_code = models.CharField(max_length=50, verbose_name="商品编码", null=True, blank=True)
    specification = models.CharField(max_length=100, verbose_name="规格型号", null=True, blank=True)
    unit = models.CharField(max_length=20, verbose_name="计量单位", null=True, blank=True)
    quantity = models.IntegerField(verbose_name="数量")
    unit_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="单价")
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="金额")
    expected_date = models.DateField(verbose_name="预计到货日期")

    class Meta:
        db_table = "purchase_detail"
        verbose_name = '采购明细'
        verbose_name_plural = verbose_name
        ordering = ('id',)
